package com.jiyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiyun.entity.Apply;
import com.jiyun.entity.Goods;
import com.jiyun.entity.User;
import com.jiyun.mapper.ApplyMapper;
import com.jiyun.mapper.BrandMapper;
import com.jiyun.mapper.GoodsMapper;
import com.jiyun.mapper.UserMapper;
import com.jiyun.service.IApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-06-06
 */
@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements IApplyService {

    @Autowired
    ApplyMapper applyMapper;

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public Page<Apply> findPage(Page<Apply> applyPage, Apply apply) {
        LambdaQueryWrapper<Apply> qw = new LambdaQueryWrapper<>();
        qw.eq(apply.getUid()!=null,Apply::getUid, apply.getUid());
        // 根据title进行模糊查询
        qw.like(StringUtils.isNotBlank(apply.getTitle()), Apply::getTitle, apply.getTitle());
        // 根据status进行查询
        if (StringUtils.isNotBlank(apply.getStatus()) && !apply.getStatus().equals("0")) {
            if (apply.getStatus().equals("5")) {
                qw.eq(Apply::getStatus, "2");
                qw.or();
                qw.eq(Apply::getStatus, "4");
            } else {
                qw.eq(Apply::getStatus, apply.getStatus());
            }
        }
        if (StringUtils.isNotBlank(apply.getTimeRange()) && !apply.getTimeRange().equals("0")) {
            String start = null;
            String end = null;
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            if(apply.getTimeRange().equals("1")){ // 今天的起始时间和结束时间
                start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).format(df);
                end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).format(df);
            }
            if(apply.getTimeRange().equals("2")){ // 昨天的起始时间和结束时间
                start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).plusDays(-1).format(df);
                end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).plusDays(-1).format(df);
            }
            if(apply.getTimeRange().equals("3")){// 本周的起始时间和结束时间
                start = LocalDateTime.of(LocalDate.now().with(DayOfWeek.MONDAY), LocalTime.MIN).format(df);
                end = LocalDateTime.of(LocalDate.now().with(DayOfWeek.SUNDAY), LocalTime.MAX).format(df);
            }
            if(apply.getTimeRange().equals("4")){// 本月的起始时间和结束时间
                start = LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN).format(df);
                end = LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX).format(df);
            }
            if(apply.getTimeRange().equals("5")){// 上个月的起始时间和结束时间
                start = LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN).plusMonths(-1).format(df);
                end = LocalDateTime.of(LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX).plusMonths(-1).format(df);
            }
            qw.ge(Apply::getApplyTime, start);
            qw.le(Apply::getApplyTime, end);
        }
        applyMapper.selectPage(applyPage, qw);
        // select * from apply a,goods g,brand b where a.gid=g.id and g.brand_id=b.id where a.uid=?
        for (Apply app : applyPage.getRecords()) {
            Goods goods = goodsMapper.selectById(app.getGid());
            goods.setBrandName(brandMapper.selectById(goods.getBrandId()).getName());
            app.setGoods(goods);
            User user = userMapper.selectById(app.getUid());
            app.setUser(user);

            if(app.getSid()!=null){
                app.setSuserName(userMapper.selectById(app.getSid()).getUsername());
            }
            if(app.getFid()!=null){
                app.setFuserName(userMapper.selectById(app.getFid()).getUsername());
            }
        }

        return applyPage;
    }

    @Override
    public void updateByIdMy(Apply apply) {
        applyMapper.updateById(apply);
    }
}
